package io.renren.modules.frt.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.frt.constant.FrtConstant;
import io.renren.modules.frt.entity.TemplateEntity;
import io.renren.modules.frt.service.TemplateService;
import io.renren.modules.sys.service.SysConfigService;

/**
 * 模板管理
 *
 * @author qibing.huang
 * @email abingtech@163.com
 * @date 2018-11-18 23:36:42
 */
@RestController
@RequestMapping("frt/template")
public class TemplateController {
	@Autowired
	private TemplateService templateService;

	@Autowired
	private SysConfigService sysConfigService;

	/**
	 * 列表
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/list")
	@RequiresPermissions("frt:template:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = templateService.queryPage(params);

		List<TemplateEntity> list = (List<TemplateEntity>) page.getList();

		List<Map> configList = sysConfigService.getConfigList(FrtConstant.TEMPLATE_TYPE_KEY, Map.class);
		if (list != null && list.size() > 0) {
			for (TemplateEntity temp : list) {
				for (Map map : configList) {
					if (MapUtils.toStr(map, "code").equals(temp.getType())) {
						temp.setTypeName(MapUtils.toStr(map, "name"));
						break;
					}
				}
			}
		}

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{tempId}")
	@RequiresPermissions("frt:template:info")
	public R info(@PathVariable("tempId") Long tempId) {
		TemplateEntity template = templateService.selectById(tempId);

		return R.ok().put("template", template);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("frt:template:save")
	public R save(@RequestBody TemplateEntity template) {
		template.setCreateTime(new Date());
		templateService.insert(template);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("frt:template:update")
	public R update(@RequestBody TemplateEntity template) {
		templateService.updateById(template);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("frt:template:delete")
	public R delete(@RequestBody Long[] tempIds) {
		templateService.deleteBatchIds(Arrays.asList(tempIds));

		return R.ok();
	}

	/**
	 * 模板类型列表
	 */
	@SuppressWarnings("rawtypes")
	@GetMapping("/select")
	@RequiresPermissions("frt:template:select")
	public R select() {
		List<Map> list = sysConfigService.getConfigList(FrtConstant.TEMPLATE_TYPE_KEY, Map.class);
		return R.ok().put("list", list);
	}
}
